package com.chuanyang.driverh.mainorder.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainorder.MatchOrderResultActivity;
import com.chuanyang.driverh.mainorder.NewPendHisOrderDetailActivity;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.responseresult.OrderDetailData;
import com.chuanyang.driverh.responseresult.PendOrderData;
import com.chuanyang.driverh.responseresult.PendOrderResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.RemoveEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class PendingOrderFragment extends BaseListFragment {
    public static boolean iscount = false;
    private OrderDetailData pushOrder;
    private DriverInfoData driverinfo;
    private PendOrderData.DataBean pendingorder;
    private PendingOrderAdapter myAdapter;
    private View headerView;
    private int mposition;
    private MyCountDownTimer countDown;
    private TextView tv_header_counttime;

    @Override
    public BaseListAdapter createAdapter() {
        return myAdapter = new PendingOrderAdapter(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        //获取存入的最新一个推送来的订单
        if (SharedPrefUtil.getPreferences( "SheveOrder",new OrderDetailData()) != null) {
            pushOrder =  SharedPrefUtil.getPreferences("SheveOrder",new OrderDetailData());
            driverinfo = SharedPrefUtil.getPreferences("DriverInfoData",new DriverInfoData());
            changPushToPending();

        }

        mCurrentPage = 1;
        getData();

    }
    @Override
    protected void initData() {
    }

    @Override
    protected void initListener() {
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    /**
     * 设置数据源
     *
     * @param list
     */
    /**
     * 设置数据源
     *
     * @param list
     */
    protected void setData(List<PendOrderData.DataBean> list) {
        if (mlistview == null) {
            return;
        }
        onShowContent();
        if (mCurrentPage == 1) {
            mAdapter.clear();
        }
        if (list != null && list.size() > 0) {
            mAdapter.addAll(list);
            mCurrentPage += 1;
        }
        else { //此次请求没有拿到数据
            if (mAdapter.getCount() > 0) {    //表示列表展示的有数据,且加载更多时没有更多
               PrintUtil.toast(getActivity(),
                        "数据已全部加载");
            } else if (myAdapter.getCount() == 0 && mlistview.getHeaderViewsCount() == 0) {//没有头，需要展示的集合长度为0

                mlistview.setEmptyView(getEmptyView());  //表示列表没数据，且请求的数据长度也为0
            }


        }
        if (isLoadMore() || isPullToRefresh()) {
            springView.onFinishFreshAndLoad();
        }
    }

    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", mCurrentPage + "");
        params.put("length", "10");

        MyHttpUtil.selectDriverPendingOrder(getActivity(),params, new MyCallback<PendOrderResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(PendOrderResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    setData(result.getData().getData());
                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    /**
     * 提供方法将PushOrder的实体对象转化为PendingOrderData对象
     */
    private void changPushToPending() {
        pendingorder = new PendOrderData.DataBean();
        pendingorder.setUserImage(pushOrder.getUserImage());
        pendingorder.setUsername(pushOrder.getUsername());
        pendingorder.setStartAddr(pushOrder.getStartAddr());
        pendingorder.setTargetAddr(pushOrder.getTargetAddr());
        pendingorder.setOrder_startime(pushOrder.getOrder_startime() + "");
        pendingorder.setOrder_endtime(pushOrder.getOrder_endtime() + "");
        pendingorder.setLoadDate(pushOrder.getLoadDate()+"");
        pendingorder.setStevedoreDay(pushOrder.getStevedoreDay()+"");
        pendingorder.setOrder_type(pushOrder.getOrder_type());
        pendingorder.setOrderId(pushOrder.getOrderId());
        pendingorder.setOrderNo(pushOrder.getOrderNo());
        pendingorder.setCapacity(pushOrder.getGoodsSumWeight());//此处是
        pendingorder.setCarStandard(pushOrder.getGoodsSumNum()+"");
        pendingorder.setGoodsName(pushOrder.getGoodsName());
        pendingorder.setTransPortWay(pushOrder.getTransPortWay());
        if (!TextUtils.isEmpty(pushOrder.getAppoint())) {
            pendingorder.setOrder_Amount(pushOrder.getOrder_Amount());
        }
        pendingorder.setReceiveTime(pushOrder.getReceiveTime());
        pendingorder.setAppoint(pushOrder.getAppoint());
        if (pushOrder.getStar() != 0) {
            pendingorder.setStar(pushOrder.getStar());

        }
        if (pendingorder != null) {
            //并且时间间隔小于60s
            if (System.currentTimeMillis() - pendingorder.getReceiveTime() < 60000) {
                //为listview单独设置一个头 避免listview的显示错乱
                fillHeader();
            }

        }

    }

    private void fillHeader() {
        headerView = View.inflate(getContext(), R.layout.order_item_pendingorder, null);
        LinearLayout ll_header_div = (LinearLayout) headerView.findViewById(R.id.ll_pendorder_div);
        ImageView iv_header_icon = (ImageView) headerView.findViewById(R.id.iv_pendorer_icon);
        ImageView iv_header_fleedpersonal = (ImageView) headerView.findViewById(R.id.iv_pendorder_fleedpersonal);//区分个人订单还是车队订单
        TextView tv_header_anme = (TextView) headerView.findViewById(R.id.tv_pendorer_anme);
        RatingBar rb_heander_stars = (RatingBar) headerView.findViewById(R.id.rb_stars);
        TextView tv_headerer_staadd = (TextView) headerView.findViewById(R.id.tv_pendorer_staadd);
        TextView tv_header_endadd = (TextView) headerView.findViewById(R.id.tv_pendorer_endadd);
        TextView tv_header_rectime = (TextView) headerView.findViewById(R.id.tv_pendorer_rectime);
        TextView tv_header_unloadtime = (TextView) headerView.findViewById(R.id.tv_pendorer_unloadtime);
        TextView tv_header_carshiprectime = (TextView) headerView.findViewById(R.id.tv_pendorer_carshiprectime);
        TextView tv_header_carshipunloadtime = (TextView) headerView.findViewById(R.id.tv_pendorer_carshipunloadtime);
        TextView tv_header_type = (TextView) headerView.findViewById(R.id.tv_pendorer_type);
        TextView tv_header_num = (TextView) headerView.findViewById(R.id.tv_pendorer_num);
        TextView tv_header_weight = (TextView) headerView.findViewById(R.id.tv_pendorer_weight);
        TextView tv_header_mount = (TextView) headerView.findViewById(R.id.tv_pendorer_mount);
        TextView tv_header_goodname = (TextView) headerView.findViewById(R.id.tv_pendorer_goodname);
        TextView tv_header_creattime = (TextView) headerView.findViewById(R.id.tv_pendorer_creattime);
        TextView tv_header_account = (TextView) headerView.findViewById(R.id.tv_pendorer_account);
        TextView tv_header_receive = (TextView) headerView.findViewById(R.id.tv_pendorer_receive);
        TextView tv_header_abandon = (TextView) headerView.findViewById(R.id.tv_pendorer_abandon);
       tv_header_counttime = (TextView) headerView.findViewById(R.id.tv_pendorer_counttime);
        TextView tv_header_desc = (TextView) headerView.findViewById(R.id.tv_pendorer_desc);
        BitmapUtil.showImgForNetPath(pendingorder.getUserImage(),iv_header_icon);
        tv_header_anme.setText(pendingorder.getUsername());
        if (pendingorder.getStar() != null) {
            rb_heander_stars.setRating(pendingorder.getStar());
        }
        tv_headerer_staadd.setText(pendingorder.getStartAddr());
        tv_header_endadd.setText(pendingorder.getTargetAddr());

        tv_header_type.setText("竞价");
        tv_header_num.setText(pendingorder.getOrderNo());
        tv_header_creattime.setText(pendingorder.getOrder_time());//后期修改,此处可能报错
        tv_header_weight.setText(pendingorder.getCapacity() + "t");
        tv_header_mount.setText(pendingorder.getCarStandard());
        setGoodName(tv_header_goodname, pendingorder.getGoodsName());
        if (pendingorder.getOrder_Amount() != null) {
            tv_header_account.setText(pendingorder.getOrder_Amount() + pendingorder.getUnit());

        } else {
            tv_header_account.setText("---");

        }
//        if (pendingorder.getFleetId() == null) {
//            iv_header_fleedpersonal.setImageResource(R.drawable.individual);
//        } else {
//            iv_header_fleedpersonal.setImageResource(R.drawable.fleetorder);
//
//        }
        if (pendingorder.getOrder_type() == 1)//竞价
        {
            tv_header_type.setText("竞价");
            tv_header_type.setBackgroundResource(R.drawable.tv_shape_order_compete);

        } else if (pendingorder.getOrder_type() == 2)//匹配
        {
            tv_header_type.setText("匹配");
            tv_header_type.setBackgroundResource(R.drawable.tv_shape_order_macher);

        } else if (pendingorder.getOrder_type() == 3)//匹配和指定运输
        {
            tv_header_type.setText("专线");
            tv_header_type.setBackgroundResource(R.drawable.tv_shape_order_special);

        }
        if (pendingorder.getTransPortWay() == 1)//车
        {
            tv_header_rectime.setText(pendingorder.getOrder_startime());
            tv_header_unloadtime.setText(pendingorder.getOrder_endtime());
            tv_header_carshiprectime.setText("接货");
            tv_header_carshipunloadtime.setText("卸货");
            if (pendingorder.getFleetId() == null) {
                iv_header_fleedpersonal.setImageResource(R.mipmap.car_transport1);
            } else {
                iv_header_fleedpersonal.setImageResource(R.mipmap.car_transport0);

            }
        } else if (pendingorder.getTransPortWay() == 2)//船
        {
            tv_header_rectime.setText(pendingorder.getLoadDate());
            tv_header_unloadtime.setText(pendingorder.getStevedoreDay());
            tv_header_carshiprectime.setText("受载日期");
            tv_header_carshipunloadtime.setText("装卸期限");
            if (pendingorder.getFleetId() == null) {
                iv_header_fleedpersonal.setImageResource(R.mipmap.shipping1);
            } else {
                iv_header_fleedpersonal.setImageResource(R.mipmap.shipping0);

            }

        }
        ll_header_div.setOnClickListener(new View.OnClickListener() {
            //"1"表示搁置的待处理订单 "2"表示普通的待处理订单  "3"表示订单历史
            @Override
            public void onClick(View v) {
                // Intent intent = new Intent(getActivity(), PendHisOrderDetailActivity.class);
                Intent intent = new Intent(getActivity(), NewPendHisOrderDetailActivity.class);
                intent.putExtra("SheveOrder", pushOrder);
                intent.putExtra("OrderId", pushOrder.getOrderId() + "");
                intent.putExtra("index", "1");
                startActivity(intent);
            }
        });
        tv_header_receive.setOnClickListener(new View.OnClickListener() {  //接受匹配订单
            @Override
            public void onClick(View v) {
                receiveOrder(pendingorder,0);

            }
        });
        tv_header_abandon.setOnClickListener(new View.OnClickListener() {//放弃待处理订单
            @Override
            public void onClick(View v) {
                adandonOrder(pendingorder, -1);
            }
        });
        if (System.currentTimeMillis() - pendingorder.getReceiveTime() < 60000) //如果时间间隔小于
        {

            tv_header_desc.setText("后将自动放弃订单");
            Long interval = System.currentTimeMillis() - pendingorder.getReceiveTime();
//            mCount = new MyCount(60000 - interval, 1000, getActivity());
//            mCount.setTextView(tv_header_counttime, PendingOrderFragment.this, 3);
//            mCount.start();

            startCountdown(60000 - interval);


            if (mlistview.getHeaderViewsCount() == 0) {
                mlistview.addHeaderView(headerView);

            }
        } else {
            headerView.setVisibility(View.GONE);
            if (mlistview.getHeaderViewsCount() > 0) {
                mlistview.removeHeaderView(headerView);

            }
        }
    }
    /**
     * 接单操作  (不是推送来的专线订单，而是列表里的专线订单 接收时传“1”)
     */
    private void receiveOrder(final PendOrderData.DataBean order, final int i) {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("orderId", order.getOrderId() + "");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("orderType", order.getOrder_type() + "");//订单类型
        if(i==1)//列表里的专线
        {
            params.put("appoint", "1");
            params.put("type",order.getType()+"");
        }
        else {//推送来的专线
            if (!TextUtils.isEmpty(order.getAppoint())) {
                params.put("appoint", order.getAppoint());
            }
            params.put("type",order.getType()+"");
        }

        MyHttpUtil.receiveOrder(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                       PrintUtil.toast(getActivity(), "接单成功");
                        Intent intent = new Intent(getActivity(), MatchOrderResultActivity.class);
                        intent.putExtra("orderNum", order.getOrderNo());
                        intent.putExtra("orderId", order.getOrderId() + "");
                        intent.putExtra("opreateType", "2");
                        if(i==0)
                        {
                            if (mlistview.getHeaderViewsCount() > 0) {
                                mlistview.removeHeaderView(headerView);
                                SharedPrefUtil.clearSp("SheveOrder");
                            }
                        }
                        else
                        {
                            mCurrentPage=1;
                            getData();
                        }
                        startActivity(intent);

                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });



    }

    /**
     * 放弃待处理订单
     */
    public void adandonOrder(final PendOrderData.DataBean order, final int position) {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("orderId", order.getOrderId() + "");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("type", order.getOrder_type() + "");

        MyHttpUtil.deletePendOrder(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    SharedPrefUtil.clearSp("SheveOrder");
                    if (position == -1) {
                        if (mlistview.getHeaderViewsCount() > 0) {//表示是存储的匹配订单
                            mlistview.removeHeaderView(headerView);
                        }

                    } else {//匹配订单 和指定订单时
                        if (position != -1) {
                            myAdapter.remove(position);
                        }
                    }
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }
    @Override
    public void onGetMore() {
        getData();
    }

    public void onEventMainThread(RemoveEvent event) {
        if (event.isRemove()) {
            if (mlistview.getHeaderViewsCount() > 0) {
                mlistview.removeHeaderView(headerView);
                SharedPrefUtil.clearSp("SheveOrder");
            }
        } else {
            myAdapter.remove(mposition);
        }
    }

    @Override
    public void onDestroy() {
        resetCount();
        super.onDestroy();
    }

    private void resetCount() {
        if (countDown != null) {
            iscount = false;
            countDown.cancel();
            countDown = null;
        }
    }

    private void setGoodName(TextView view, List<String> goodname) {
        String name = StringUtil.getString(goodname);
        view.setText(name);
    }

    private class PendingOrderAdapter extends BaseListAdapter<PendOrderData.DataBean> {
        private ViewHolder holder;
        private PendOrderData.DataBean bean;

        public PendingOrderAdapter(Context context) {
            super(context);
        }

        @Override
        public View getZkView(final int position, View convertView, ViewGroup parent) {
            mposition = position;
            if (convertView == null)
            {
                convertView = getInflater().inflate(R.layout.order_item_pendingorder, parent, false);
                holder = new ViewHolder();
                holder.ll_pendorder_div= (LinearLayout) convertView.findViewById(R.id.ll_pendorder_div);
                holder.iv_pendorer_icon = (ImageView) convertView.findViewById(R.id.iv_pendorer_icon);
                holder.iv_pendorder_fleedpersonal = (ImageView) convertView.findViewById(R.id.iv_pendorder_fleedpersonal);//区分个人订单还是车队订单
                holder.tv_pendorer_anme = (TextView) convertView.findViewById(R.id.tv_pendorer_anme);
                holder.rb_stars = (RatingBar) convertView.findViewById(R.id.rb_stars);
                holder.tv_pendorer_staadd = (TextView) convertView.findViewById(R.id.tv_pendorer_staadd);
                holder.tv_pendorer_endadd = (TextView) convertView.findViewById(R.id.tv_pendorer_endadd);
                holder.tv_pendorer_carshiprectime = (TextView) convertView.findViewById(R.id.tv_pendorer_carshiprectime);
                holder.tv_pendorer_carshipunloadtime = (TextView) convertView.findViewById(R.id.tv_pendorer_carshipunloadtime);
                holder.tv_pendorer_rectime = (TextView) convertView.findViewById(R.id.tv_pendorer_rectime);
                holder.tv_pendorer_unloadtime = (TextView) convertView.findViewById(R.id.tv_pendorer_unloadtime);
                holder.tv_pendorer_type = (TextView) convertView.findViewById(R.id.tv_pendorer_type);
                holder.tv_pendorer_num = (TextView) convertView.findViewById(R.id.tv_pendorer_num);
                holder.tv_pendorer_weight = (TextView) convertView.findViewById(R.id.tv_pendorer_weight);
                holder.tv_pendorer_mount = (TextView) convertView.findViewById(R.id.tv_pendorer_mount);//数量
                holder.tv_pendorer_goodname = (TextView) convertView.findViewById(R.id.tv_pendorer_goodname);
                holder.tv_pendorer_creattime = (TextView) convertView.findViewById(R.id.tv_pendorer_creattime);
                holder.tv_pendorer_account = (TextView) convertView.findViewById(R.id.tv_pendorer_account);
                holder.tv_pendorer_receive = (TextView) convertView.findViewById(R.id.tv_pendorer_receive);
                holder.tv_pendorer_abandon = (TextView) convertView.findViewById(R.id.tv_pendorer_abandon);
                holder.tv_pendorer_counttime = (TextView) convertView.findViewById(R.id.tv_pendorer_counttime);
                holder.tv_pendorer_desc = (TextView) convertView.findViewById(R.id.tv_pendorer_desc);
                holder.tv_pendorer_tradetype = (TextView) convertView.findViewById(R.id.tv_pendorer_tradetype);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            bean = getItem(position);
            BitmapUtil.showImgForNetPath(bean.getUserImage(),holder.iv_pendorer_icon);
            holder.tv_pendorer_anme.setText(bean.getUsername());
            holder.tv_pendorer_staadd.setText(bean.getStartAddr());
            holder.tv_pendorer_endadd.setText(bean.getTargetAddr());
            holder.tv_pendorer_num.setText(bean.getOrderNo());
            holder.tv_pendorer_creattime.setText(bean.getOrder_time());
            if(bean.getGoodsSum()!=null) {
                holder.tv_pendorer_mount.setText(bean.getGoodsSum().getGoodsNum()+"");
                holder.tv_pendorer_weight.setText(bean.getGoodsSum().getGoodsWeight() + "t");

            }
            else {
                holder.tv_pendorer_mount.setText("---");
                holder.tv_pendorer_weight.setText("---");


            }
            setGoodName(holder.tv_pendorer_goodname, bean.getGoodsName());
            holder.tv_pendorer_counttime.setText("");
            holder.tv_pendorer_receive.setVisibility(View.INVISIBLE);
            if(bean.getCurrency()==1)
            {
                holder.tv_pendorer_tradetype.setVisibility(View.GONE);
            }
            else {
                holder.tv_pendorer_tradetype.setVisibility(View.VISIBLE);

            }
            if (bean.getTransPortWay() == 1)//车
            {
                holder.tv_pendorer_rectime.setText(bean.getOrder_startime());
                holder.tv_pendorer_unloadtime.setText(bean.getOrder_endtime());
                holder.tv_pendorer_carshiprectime.setText("接货");
                holder.tv_pendorer_carshipunloadtime.setText("卸货");
                if (bean.getFleetId() == null) {
                    holder.iv_pendorder_fleedpersonal.setImageResource(R.mipmap.car_transport1);
                } else {
                    holder.iv_pendorder_fleedpersonal.setImageResource(R.mipmap.car_transport0);

                }
            } else if (bean.getTransPortWay() == 2)//船
            {
                holder.tv_pendorer_rectime.setText(bean.getLoadDate());
                holder.tv_pendorer_unloadtime.setText(bean.getStevedoreDay());
                holder.tv_pendorer_carshiprectime.setText("受载日期");
                holder.tv_pendorer_carshipunloadtime.setText("装卸期限");
                if (bean.getFleetId() == null) {
                    holder.iv_pendorder_fleedpersonal.setImageResource(R.mipmap.shipping1);
                } else {
                    holder.iv_pendorder_fleedpersonal.setImageResource(R.mipmap.shipping0);

                }

            }
            if (bean.getOrder_status() != null && bean.getOrder_status() == 0) {
                holder.tv_pendorer_desc.setTextColor(getResources().getColor(R.color.gray_66));
                holder.tv_pendorer_desc.setText("正在等待货主选择");

            }
            else {

                holder.tv_pendorer_desc.setTextColor(getResources().getColor(R.color.red));
                holder.tv_pendorer_desc.setText("异常订单");

            }

            if (bean.getOrder_Amount() != null) {
                holder.tv_pendorer_account.setText(bean.getOrder_Amount() + bean.getUnit());

            } else {
                holder.tv_pendorer_account.setText("---");

            }
            if (bean.getStar() != null) {
                holder.rb_stars.setRating(bean.getStar());
            }
            if (bean.getOrder_type() == 1)//竞价
            {
                holder.tv_pendorer_type.setText("竞价");
                holder.tv_pendorer_type.setBackgroundResource(R.drawable.tv_shape_order_compete);
                holder.tv_pendorer_tradetype.setBackgroundColor(getResources().getColor(R.color.compete));
                holder.tv_pendorer_abandon.setText("放弃");


            } else if (bean.getOrder_type() == 2)//匹配
            {
                holder.tv_pendorer_type.setText("匹配");
                holder.tv_pendorer_type.setBackgroundResource(R.drawable.tv_shape_order_macher);
                holder.tv_pendorer_tradetype.setBackgroundColor(getResources().getColor(R.color.match));
                holder.tv_pendorer_abandon.setText("放弃");


            } else if (bean.getOrder_type() == 3)//匹配和指定运输
            {
                holder.tv_pendorer_type.setText("专线");
                holder.tv_pendorer_type.setBackgroundResource(R.drawable.tv_shape_order_special);
                holder.tv_pendorer_abandon.setText("接受");
                holder.tv_pendorer_tradetype.setBackgroundColor(getResources().getColor(R.color.specialline));

                // holder.tv_pendorer_receive.setVisibility(View.VISIBLE);

            }
            holder.tv_pendorer_abandon.setOnClickListener(new View.OnClickListener() {//放弃竞价
                @Override
                public void onClick(View v)
                {
                    bean=getItem(position);

                    if(bean.getOrder_type()==3)
                    {
                        receiveOrder(bean,1);

                    }
                    else {
                        adandonOrder(bean, position);
                    }
                }
            });
//            holder.tv_pendorer_receive.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    bean=getItem(position);
//                    receiveOrder(bean,1);
//
//                }
//            });
            holder.ll_pendorder_div.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bean=getItem(position);
                    //   Intent intent = new Intent(getActivity(), PendHisOrderDetailActivity.class);
                    Intent intent = new Intent(getActivity(), NewPendHisOrderDetailActivity.class);
                    intent.putExtra("OrderId", bean.getOrderId() + "");
                    intent.putExtra("index", "2");
                    intent.putExtra("type", bean.getType()+"");
                    startActivity(intent);
                }
            });
            return convertView;
        }


        class ViewHolder {
            LinearLayout ll_pendorder_div;
            ImageView iv_pendorer_icon;

            ImageView iv_pendorder_fleedpersonal;
            TextView tv_pendorer_anme;
            // TextView  tv_myorder_type;//运单还是订单
            TextView tv_pendorer_staadd;
            // TextView  tv_myorder_passadd;
            TextView tv_pendorer_endadd;
            TextView tv_pendorer_carshiprectime;
            TextView tv_pendorer_carshipunloadtime;
            TextView tv_pendorer_rectime;
            TextView tv_pendorer_unloadtime;
            TextView tv_pendorer_type;
            TextView tv_pendorer_num;
            TextView tv_pendorer_weight;
            TextView tv_pendorer_mount;
            TextView tv_pendorer_goodname;
            TextView tv_pendorer_creattime;
            TextView tv_pendorer_account;
            TextView tv_pendorer_receive;
            TextView tv_pendorer_abandon;
            TextView tv_pendorer_counttime;
            TextView tv_pendorer_desc;
            TextView tv_pendorer_tradetype;//外贸内贸
            RatingBar rb_stars;
        }

    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    private void startCountdown(long time) {
        countDown = new MyCountDownTimer(time, 1000);
        countDown.start();
        iscount = true;
    }
    class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            super.cancel();
            if (mlistview.getHeaderViewsCount() > 0) {
                mlistview.removeHeaderView(headerView);
                SharedPrefUtil.clearSp("SheveOrder");
            }
//            tv_grborder_timecount
//            bt_code.setText("获取验证码");
//            bt_code.setBackgroundResource(R.drawable.round_corner_whitetrans_shape);
//            bt_code.setEnabled(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_header_counttime.setText((millisUntilFinished / 1000)+"");
//            bt_code.setEnabled(false);
//            bt_code.setBackgroundResource(R.drawable.round_corner_whitegray_shape);
        }
    }
}
